<?


if (!defined("MCTDESIGN")) die("# ");

if (isset($_POST['delete'])) {

	$total = isset($_POST["total"]) ? intval($_POST["total"]) : 0;

 	for($i=1;$i<=$total;$i++){

		$checkbox[$i] = isset($_POST["checkbox_$i"]) ? $_POST["checkbox_$i"] : '';

		if (!empty($checkbox[$i])) {

			$mysql->query("DELETE FROM ".$tb_fix."_manufacturer WHERE manu_id='".$checkbox[$i]."'");

		}

	}

	$html_body = _html_trans("Đã xoá các hãng sản xuất thành công !");

	return false;

}

if (isset($_POST['order'])) {

	$total = isset($_POST["total"]) ? intval($_POST["total"]) : 0;

    for ($i=1;$i<=$total;$i++)

      {

           $manu_id    = isset($_POST["order_id_$i"])    ? $_POST["order_id_$i"]    : 0;

           $manu_order = isset($_POST["order_$i"]) ? $_POST["order_$i"] : 0;

           if ( ($manu_id>0)&&($manu_order>0) )

           {		

				$mysql->query("UPDATE ".$tb_fix."_manufacturer SET manu_order='$manu_order' WHERE manu_id='".$manu_id."'");

           }

    }

	$html_body = _html_trans("Đã cập nhật thứ tự các hãng sản xuất thành công !");

	return false;

}

if (isset($_POST['update'])) {

	$manu_title = isset($_POST["manu_title"]) ? fixquotes(stripslashes($_POST["manu_title"])) : "";

	$manu_parent = isset($_POST["manu_parent"]) ? intval(stripslashes($_POST["manu_parent"])) : 0;

	$manu_description = isset($_POST["manu_description"]) ? fixquotes(stripslashes($_POST["manu_description"])) : "";

	if (empty($manu_title)) {

		$html_body = _html_trans("Tên của hãng sản xuất không được bỏ trống !");

		return false;

	}

    $mysql->query("UPDATE ".$tb_fix."_manufacturer SET manu_title='".$manu_title."', manu_description='$manu_description',manu_parent='$manu_parent' WHERE manu_id='".$id."'");

	$html_body = _html_trans("Đã cập nhật thông tin hãng sản xuất thành công !","?obj=Manufacturer");

	return false;

}

if (isset($_POST['add'])) {

	$manu_title = isset($_POST["manu_title"]) ? fixquotes(stripslashes($_POST["manu_title"])) : "";

	$manu_parent = isset($_POST["manu_parent"]) ? intval(stripslashes($_POST["manu_parent"])) : 0;

	$manu_description = isset($_POST["manu_description"]) ? fixquotes(stripslashes($_POST["manu_description"])) : "";

	if (empty($manu_title)) {

		$html_body = _html_trans("Bạn phải nhập tên của hãng sản xuất !");

		return false;

	}

	$q = $mysql->query("SELECT * FROM ".$tb_fix."_manufacturer WHERE manu_parent='$manu_parent' ORDER BY manu_order DESC");

	$r = $mysql->num_rows($q);

	$order = $r+1;

	$mysql->query("INSERT INTO ".$tb_fix."_manufacturer (manu_title ,manu_description ,manu_parent ,manu_order ) VALUES ('$manu_title','$manu_description','$manu_parent','$order')");

	$html_body = _html_trans("Đã thêm hãng sản xuất mới thành công !");

	return false;

}

$_c = 0;

function show_managerCategory($parent="0", $space=0){
	global $mysql, $tb_fix, $_c;
    $_limit = 15;
    $page = isset($_GET["page"]) ? intval($_GET["page"]) : 1;
	$q = $mysql->query("SELECT * FROM ".$tb_fix."_manufacturer ORDER BY manu_order ASC");
    $t = $mysql->num_rows($q);
    if($t>0){
            $_start = ($page-1) * $_limit ;
			$_end   = $page*$_limit;
			$num_pages = ceil($t/$_limit) ;
			if ($page > $num_pages) $page=$num_pages;
			if ($page < 1 ) $page=1;
			$page_view = Paginate_Admin($t, $_limit, $page, "page");
			$i = 0;
            $html="";
    	while ($r = $mysql->fetch_array($q)) {
    	   if ($i>=$_start) {
    			$_c++;
    			if ($r["manu_parent"]==0) {
    				$_b = "<b>";
    				$space = "";
    			}
    			else {
    				$_b = "";
    				$space = "&nbsp;&nbsp;&nbsp;&nbsp;";
    			}
                $html .= "<tr class=\"row1\">";
                $html .= "<td align=\"center\" width=\"5%\" style=\"text-align:center;border-bottom: 1px solid #302F29;\"><input type='checkbox' id='checkbox' name='checkbox_".$_c."' value='".$r["manu_id"]."'></td>";
    			$html .= "<td style='border-bottom: 1px solid #302F29;' align=\"center\" width=\"12%\">".$space."<input name='order_id_".$_c."' type='hidden' value='".$r["manu_id"]."'> <input name='order_".$_c."' type='text' size='3' maxlength='15' value='".$r["manu_order"]."'></td>";
    			$html .= "<td style='border-bottom: 1px solid #302F29;' width=\"75%\" align=\"left\" id=\"manu_".$r["manu_id"]."\" ><div style=\"margin-left:5px;\">".$space."<a href=\"?obj=Manufacturer&act=edit&id=".$r["manu_id"]."\"><b>".$r["manu_title"]."</b></a></span></div></td>";
    			$html .= "<td width=\"13%\" id=\"adm_action\">
                        <div id='adm_edit' title='sửa nội dung' onclick=\"redirect('?obj=Manufacturer&act=edit&id=".$r["manu_id"]."')\"></div>
                        <div id='adm_del' title='xóa nội dung' onclick='check_del_row(".$r["manu_id"].")'></div>
                        </td>";
    			$html .= "</tr>";
          }
          $i++;
	       if ($i>=$_end) break;      
    	}
    }
	return array($html,$page_view);
}
function _html_manageCategory()
{
	global $dir_tpl, $mysql, $tb_fix,$admin_id,$admin_permissions,$_c,$dir_tpl,$temp;
	$temp_body = $temp;
	list($list_manufacturer,$page_view) = show_managerCategory($parent="0", $space="");
	$list_manufacturer .= "<input type='hidden' name='total' value='".$_c."'>";
	$list_manufacturer .= "<tr class=\"row1\">";
	$list_manufacturer .= "<td align=\"center\"><input name='delete' type='submit' value=' Xoá ' onClick=\"return check_checkbox();\"></td>";
    $list_manufacturer .= "<td align=\"center\"><input name='order' type='submit' value='Thứ Tự'></td>";
	$list_manufacturer .= "</tr>";
	$temp_body->assign( 'list_manufacturer', "".$list_manufacturer."" );
    $temp_body->assign( 'page_view', "".$page_view."" );
    $temp_body->assign('dir_tpl',$dir_tpl);
    $temp_body->assign("breakcum",breakcum("Quản lý hãng sản phẩm "));
	$_html = $temp_body->fetch( '_adm_manufacturer.mct' );
	return $_html;
}

function _html_addCategory()

{

	global $dir_tpl, $mysql, $tb_fix, $temp;

	$temp_body = $temp;

	$temp_body->assign( 'base_tpl', "".$dir_tpl."" );

	$_html = $temp_body->gethtml( '_adm_admin_add.mct' );

	return $_html;

}

function _html_editCategory($_id="")

{

	global $dir_tpl, $mysql, $tb_fix,$admin_id,$admin_permissions,$temp;

	$q = $mysql->query("SELECT * FROM ".$tb_fix."_manufacturer WHERE manu_id='$_id' ORDER BY manu_id ASC LIMIT 1");

	$t = $mysql->num_rows($q);

	if ($t>0) {

		$r = $mysql->fetch_array($q);

		$temp_body = $temp;

		$temp_body->assign(array(

								"manu_title" => "".$r["manu_title"]."",

								"manu_description" => "".$r["manu_description"]."",

								"manu_id" => "".$r["manu_id"].""

							)

						);
        $temp_body->assign("breakcum",breakcum("Sửa hãng sản phẩm "));
		$_html = $temp_body->fetch( '_adm_manufacturer_edit.mct' );

	}

	return $_html;

}

if ($act=="edit") {

	$html_body = _html_editCategory($id);

	return false;

}

$html_body = _html_manageCategory();



?>